// pages/coin/coin.js
import {
    requestUtil,
    getbaseUrl
} from "../../utils/request"

import {getFloatStr} from "../../utils/util"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        hei: 0,
        baseUrl: "",
        userInfo: {},
        money: 0,
        qactive: 0, //签到
        dactive: 0, //订单,
        money:0
    },


    
    // 跳到管理页
    toManage(){
        wx.navigateTo({
          url: '/pages/manage/manage',
        })
    },

    // 跳到充值页面
    toRecharge(){
        // console.log("fesfes")
        wx.navigateTo({
          url: '/pages/recharge/recharge',
        })
    },

    // 跳到条形码
    toBarcode(){
        wx.navigateTo({
          url: '/pages/barcode/barcode',
        })
    },

    // 任务操作 签到
    bot(e) {
        let {
            type,
            coinnum
        } = e.currentTarget.dataset
        // console.log(e.currentTarget.dataset)
        let openId = this.data.userInfo.openid
        if (type == 1) {
           this.deduction(openId,coinnum)
            this.getUserMoney()
            this.setData({
                qactive: type
            })
        }
        if (type == 2) {
            this.deduction(openId,coinnum)
            this.getUserMoney()
            this.setData({
                dactive: type
            })
        }
    },

    // 获取用户的金额
    getUserMoney() {
        let openId = this.data.userInfo.openid
        let res = requestUtil({
            url: "/shop/getUserMoney",
            method: "get",
            data: {
                openId
            }
        })
        res.then(({
            data
        }) => {
            // console.log(data)
            let totalPrice = data.data.reduce((total, item) => {
                return total += item.money
            }, 0)
            if (data.status == 200) {
                this.setData({
                    // money: getFloatStr(totalPrice)
                    money: totalPrice
                })
            } else {
                this.setData({
                    money: 0
                })
            }
        })
    },

    // 获取高度
    getHei() {
        wx.getSystemInfo({
            success: (result) => {
                //   console.log(result.windowHeight)
                this.setData({
                    hei: result.windowHeight
                })
            },
        })
    },

    // 狗加钱
    deduction(openId,totalPrice){
          requestUtil({
            url:"/shop/noMatchPassword",
            method:"get",
            data:{
              openId,
              money:totalPrice
            }
          })
       
      },

    // 返回上一页
    toback() {
        wx.navigateBack({
            delta: 1,
        })
    },


    // 获取用户的信息

    getUserInfo() {
        wx.getStorage({
            key: "userInfo",
            success: (res) => {
                let data = JSON.parse(res.data)
                this.setData({
                    userInfo: data
                })
                this.getUserMoney()
            }
        })
    },

    // 获取用户的金额
    getUserMoney(){
        let openId = this.data.userInfo.openid
        let res = requestUtil({
            url:"/shop/getUserMoney",
            method:"get",
            data:{
                openId
            }
        })
        res.then(({data})=>{
            // console.log(data)
            let totalPrice = data.data.reduce((total,item)=>{
               return total += item.money
            },0)
            if(data.status==200){
                this.setData({
                    money:totalPrice.toFixed(2)
                })
            }else{
                this.setData({
                    money:0
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getHei()
        this.setData({
            baseUrl: getbaseUrl() + "/image/"
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})